package com.windy.dailynews.presenter.impl;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.windy.dailynews.model.Themes;
import com.windy.dailynews.presenter.SlideMenuContract;
import com.windy.dailynews.request.DataRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by dingfeng on 2016/11/22.
 */
public class SlideMenuPresenter implements SlideMenuContract.Presenter {

    private final SlideMenuContract.View mView;

    public SlideMenuPresenter(SlideMenuContract.View view) {
        mView = view;
        mView.setPresenter(this);
    }

    @Override
    public void loadData() {
        DataRequest.getInstance().getMenuList(new DataRequest.Callback<JSONObject>() {
            @Override
            public void onSuccess(JSONObject data) {
                Gson gson = new Gson();
                try {
                    JSONArray others = data.getJSONArray("others");
                    List<Themes> themes = gson.fromJson(others.toString(),
                            new TypeToken<List<Themes>>() {
                            }.getType());
                    mView.addMenu(themes);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(String err) {

            }
        });
    }

    @Override
    public void start() {

    }

}
